package com.hmeter.hadoop.hbase;

import java.io.IOException;

import org.apache.jmeter.samplers.AbstractSampler;
import org.apache.jmeter.samplers.Entry;
import org.apache.jmeter.samplers.SampleResult;

public class HBaseDeleteSampler extends AbstractSampler {

	/**
	 * 
	 */
	private static final long serialVersionUID = 38984348210549281L;
	
	private static final String ZOOKEEPERQUORUM = "ZOOKEEPERQUORUM";
	private static final String HBASETABLE = "HBASETABLE";
	private static final String ROWKEY = "ROWKEY";
	private static final String FULLCOLUMNQUALIFIERS = "FULLCOLUMNQUALIFIERS";
	private static final String COLUMNFAMILIES = "COLUMNFAMILIES";
	private static final String FILTERCOLUMNFAMILIES = "FILTERCOLUMNFAMILIES";
	private static final String DELIMITER = "DELIMITER";
	private static final String DELETECOLUMNQUALIFIERSBOOL = "DELETECOLUMNQUALIFIERSBOOL";
	private static final String DELETECOLUMNFAMILIESBOOL = "DELETECOLUMNFAMILIESBOOL";
	
	@Override
	public SampleResult sample(Entry e) {
		SampleResult result = new SampleResult();
		
		String requestData = "zookeeper quorum: " + this.getZookeeperQuorum() + "\n";
		requestData += "hbase table: " + this.getHBaseTable() + "\n";
		requestData += "row key: " + this.getRowKey() + "\n";
		requestData += "full column qualifiers to delete: " + this.getFullColumnQualifiers() + "\n";
		requestData += "column families to delete: " + this.getColumnFamilies() + "\n";
		requestData += "filter column families: " + this.getFilterColumnFamilies() + "\n";
		requestData += "delimiter: " + this.getDelimiter() + "\n";
		requestData += "deleting column qualifiers: " + this.isDeleteColumnQualifiersBool() + "\n";
		requestData += "deleting column families: " + this.isDeleteColumnFamiliesBool() + "\n";
		
		result.setSampleLabel(getName());
		result.setSamplerData(requestData);
		result.setDataType(SampleResult.TEXT);

		result.sampleStart();
		
		if (this.isDeleteColumnQualifiersBool())
		{
			try {
				HBaseDeleteSamplerUtility hbaseDelete = new HBaseDeleteSamplerUtility(this.getZookeeperQuorum(), this.getHBaseTable());
				String response = hbaseDelete.deleteColumnQualifiersFromHBase(this.getRowKey(), this.getFullColumnQualifiers(), this.getFilterColumnFamilies(), this.getDelimiter());
				result.setResponseData(response.getBytes());
				result.setSuccessful(true);
			} catch (IOException e1) {
				result.setResponseData(e1.getMessage().getBytes());
				result.setSuccessful(false);
			}
		}
		
		if (this.isDeleteColumnFamiliesBool())
		{
			try {
				HBaseDeleteSamplerUtility hbaseDelete = new HBaseDeleteSamplerUtility(this.getZookeeperQuorum(), this.getHBaseTable());
				String response = hbaseDelete.deleteColumnFamiliesDataFromHBase(this.getRowKey(), this.getColumnFamilies(), this.getFilterColumnFamilies(), this.getDelimiter());
				result.setResponseData(response.getBytes());
				result.setSuccessful(true);
			} catch (IOException e1) {
				result.setResponseData(e1.getMessage().getBytes());
				result.setSuccessful(false);
			}
		}
			
		result.sampleEnd();
		return result;
	}
	
	public void setZookeeperQuorum(String zookeeperQuorum)
	{
		setProperty(ZOOKEEPERQUORUM, zookeeperQuorum);
	}
	
	public String getZookeeperQuorum()
	{
		return getPropertyAsString(ZOOKEEPERQUORUM);
	}
	
	public void setHBaseTable(String hbaseTable)
	{
		setProperty(HBASETABLE, hbaseTable);
	}
	
	public String getHBaseTable()
	{
		return getPropertyAsString(HBASETABLE);
	}
	
	public void setRowKey(String rowKey)
	{
		setProperty(ROWKEY, rowKey);
	}
	
	public String getRowKey()
	{
		return getPropertyAsString(ROWKEY);
	}
	
	public void setFullColumnQualifiers(String fullColumnQualifiers)
	{
		setProperty(FULLCOLUMNQUALIFIERS, fullColumnQualifiers);
	}
	
	public String getFullColumnQualifiers()
	{
		return getPropertyAsString(FULLCOLUMNQUALIFIERS);
	}
	
	public void setColumnFamilies(String columnFamilies)
	{
		setProperty(COLUMNFAMILIES, columnFamilies);
	}
	
	public String getColumnFamilies()
	{
		return getPropertyAsString(COLUMNFAMILIES);
	}
	
	public void setFilterColumnFamilies(String filterColumnFamilies)
	{
		setProperty(FILTERCOLUMNFAMILIES, filterColumnFamilies);
	}
	
	public String getFilterColumnFamilies()
	{
		return getPropertyAsString(FILTERCOLUMNFAMILIES);
	}
	
	public void setDelimiter(String delimiter)
	{
		setProperty(DELIMITER, delimiter);
	}
	
	public String getDelimiter()
	{
		return getPropertyAsString(DELIMITER);
	}
	
	public boolean isDeleteColumnQualifiersBool()
	{
		return getPropertyAsBoolean(DELETECOLUMNQUALIFIERSBOOL);
	}
	
	public void setDeleteColumnQualifiersBool(boolean deleteColumnQualifiers)
	{
		setProperty(DELETECOLUMNQUALIFIERSBOOL, deleteColumnQualifiers);
	}
	
	public boolean isDeleteColumnFamiliesBool()
	{
		return getPropertyAsBoolean(DELETECOLUMNFAMILIESBOOL);
	}
	
	public void setDeleteColumnFamiliesBool(boolean deleteColumnFamilies)
	{
		setProperty(DELETECOLUMNFAMILIESBOOL, deleteColumnFamilies);
	}

}
